﻿//********************************************************************
// 
//  file:  frmCreateAccount.cs
// 
//  (C) Copyright 2010, Diomede Corporation
//  All rights reserved
// 
//  Use, modification, and distribution is subject to   
//  the New BSD License (See accompanying file license.txt).
// 
//********************************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Diomede.SDK;
using System.Configuration;

namespace Diomede.Explorer
{
    public partial class frmCreateAccount : Form
    {
        public frmCreateAccount()
        {
            InitializeComponent();
        }

        private void frmCreateAccount_Load(object sender, EventArgs e)
        {
            picLoading.Visible = false;
            lblLoading.Visible = false;
            loadEndpoints();
        }

        public string ServiceUrl { get; set; }
        public string TransferUrl { get; set; }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            //let the service validates the input
            //if (!isValidInputs())
            //{
            //    MessageBox.Show("All fields need to be complete to create an account.", "Account Creation");
            //    return;
            //}

            picLoading.Visible = true;
            lblLoading.Visible = true;
            this.Refresh();

            ISession session;

            if (cboEndpoint.SelectedIndex >= 0)
            {
                CustomEndpoint endpoint = (CustomEndpoint)cboEndpoint.SelectedItem;
                ServiceUrl = endpoint.ServiceUrl;
                TransferUrl = endpoint.TransferUrl;
                session = new Session(true, ServiceUrl, TransferUrl);
            }
            else
            {
                session = new Session();
            }


             
            IAccountManagement ac = session as IAccountManagement;
            ISessionExecResult result = ac.Create(txtUsername.Text, txtPassword.Text, txtEmail.Text);
            picLoading.Visible = false;
            lblLoading.Visible = false;
            if (result.Status != Diomede.SDK.Enums.ExecutionResults.Success)
            {
                MessageBox.Show(this, result.Message, "Error Creating Account");
            }
            else
            {
                MessageBox.Show(this, "User successfully created.");
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        bool isValidInputs()
        {
            if (string.IsNullOrEmpty(txtUsername.Text))
            {
                //MessageBox.Show(this, "Invalid username.");
                return false;
            }

            if (string.IsNullOrEmpty(txtEmail.Text))
            {
                //MessageBox.Show(this, "Invalid email.");
                return false;
            }

            if (string.IsNullOrEmpty(txtPassword.Text))
            {
                //MessageBox.Show(this, "Invalid password.");
                return false;
            }
            if (txtPassword.Text != txtConfirmPassword.Text)
            {
                //MessageBox.Show(this, "Invalid password confirmation.");
                return false;
            }
            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtConfirmPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnCreate_Click(null, null);
            }
        }

        private void btnAddEndpoint_Click(object sender, EventArgs e)
        {
            frmEndpoint frm = new frmEndpoint();
            if (frm.ShowDialog(this) == DialogResult.OK)
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                config.AppSettings.Settings.Add("customEndpoint", new CustomEndpoint { Name = frm.DisplayName, ServiceUrl = frm.ServiceUrl, TransferUrl = frm.TransferUrl }.ToSaveFormat());
                config.Save(ConfigurationSaveMode.Full);
                loadEndpoints();
            }
        }

        void loadEndpoints()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (config.AppSettings.Settings["customEndpoint"] == null)
            {
                config.AppSettings.Settings.Add("customEndpoint", new CustomEndpoint { Name = "Production", ServiceUrl = "http://service.diomedestorage.com", TransferUrl = "http://transfer.diomedestorage.com" }.ToSaveFormat());
                config.AppSettings.Settings.Add("customEndpoint", new CustomEndpoint { Name = "Test", ServiceUrl = "http://diomedetest:88/DiomedeStorageService.svc", TransferUrl = "http://diomedetest:88/DiomedeStoragetransfer.svc" }.ToSaveFormat());
                config.Save(ConfigurationSaveMode.Full);
            }

            cboEndpoint.Items.Clear();
            string[] values = config.AppSettings.Settings["customEndpoint"].Value.Split(",".ToCharArray());
            foreach (string item in values)
            {
                cboEndpoint.Items.Add(new CustomEndpoint(item));
            }
            cboEndpoint.DropDownStyle = ComboBoxStyle.DropDown;
            cboEndpoint.Refresh();
            cboEndpoint.SelectedIndex = 0;
        }
    }
}
